/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.app;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.app.EleSourceApp;
import com.thinkgem.jeesite.modules.source.service.app.EleSourceAppService;

/**
 * 设备APP管理Controller
 * @author ZC
 * @version 2017-07-04
 */
@Controller
@RequestMapping(value = "${adminPath}/source/app/eleSourceApp")
public class EleSourceAppController extends BaseController {

	@Autowired
	private EleSourceAppService eleSourceAppService;
	
	@ModelAttribute
	public EleSourceApp get(@RequestParam(required=false) String id) {
		EleSourceApp entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceAppService.get(id);
		}
		if (entity == null){
			entity = new EleSourceApp();
		}
		return entity;
	}
	
	@RequiresPermissions("source:app:eleSourceApp:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceApp eleSourceApp, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceApp> page = eleSourceAppService.findPage(new Page<EleSourceApp>(request, response), eleSourceApp); 
		model.addAttribute("page", page);
		return "modules/source/app/eleSourceAppList";
	}

	@RequiresPermissions("source:app:eleSourceApp:view")
	@RequestMapping(value = "form")
	public String form(EleSourceApp eleSourceApp, Model model) {
		model.addAttribute("eleSourceApp", eleSourceApp);
		return "modules/source/app/eleSourceAppForm";
	}

	@RequiresPermissions("source:app:eleSourceApp:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceApp eleSourceApp, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceApp)){
			return form(eleSourceApp, model);
		}
		eleSourceAppService.save(eleSourceApp);
		addMessage(redirectAttributes, "保存设备APP成功");
		return "redirect:"+Global.getAdminPath()+"/source/app/eleSourceApp/?repage";
	}
	
	@RequiresPermissions("source:app:eleSourceApp:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceApp eleSourceApp, RedirectAttributes redirectAttributes) {
		eleSourceAppService.delete(eleSourceApp);
		addMessage(redirectAttributes, "删除设备APP成功");
		return "redirect:"+Global.getAdminPath()+"/source/app/eleSourceApp/?repage";
	}

}